// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: xguo

#pragma once

#include <cstdio>
#include <memory>
#include <set>
#include <string>
#include <map>

#include <rapidjson/document.h>

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/mexc_futures/symbology/product.h"

namespace coin2::exchange::mexc_futures::order_v1 {

class MexcFuturesParser : public base::order::BaseParser {
 public:
  using TransferType = coin::proto::TransferRequestProto::TransferType;

 public:
  MexcFuturesParser() = default;
  const ParseResult& ParseRawMessage(const RawMessage& msg) override;
  AccountBalance ParseBalance(const JsonValue& doc) override;
  AccountPosition ParsePosition(const JsonValue& doc) override;
  bool ParseRawBalance(const JsonValue& doc, RawBalanceMap* raw_balance_map);
  bool ParseRawPosition(const JsonValue& doc, RawPositionMap* raw_position_map);

 private:
  void ParseSubmitResponse();
  void ParseCancelResponse();
  void ParseRestOpenOrders();
  void ParseRestAllOrders();
  void ParseRestFillInfoList();
  void ParseRestAccountBalance();
  void ParseRestAccountPosition();
  void ParseWsMessage();

  void ParseRestOrderInfoList();
  bool ParseWsPosition(const JsonValue& doc, AccountPosition*);
  ProductOrderBundle ParseWsOrderInfoList(const JsonValue& data);
  ProductFillBundle ParseWsFillInfoList(const JsonValue& data);
  coin::proto::ProductOrderElement ParseRestOrderInfo(const JsonValue& data);
  bool ParseRawPositionFromWs(const JsonValue& doc, RawPositionMap* raw_position_map);

 private:
  bool is_account_balance_initialized_ = false;
  bool is_account_position_initialized_ = false;
};

}  // namespace coin2::exchange::mexc_futures::order_v1
